package com.wangjie.utils;

import java.text.DecimalFormat;

/**
 * 文件工具类
 *
 * @author jieshao
 * @date 2021/11/26 14:20
 */
public class FileUtils {
    public static final String IMAGE = "图片";
    public static final String TXT = "文档";
    public static final String MUSIC = "音乐";
    public static final String VIDEO = "视频";
    public static final String OTHER = "其他";

    /**
     * 定义GB的计算常量
     */
    private static final int GB = 1024 * 1024 * 1024;
    /**
     * 定义MB的计算常量
     */
    private static final int MB = 1024 * 1024;
    /**
     * 定义KB的计算常量
     */
    private static final int KB = 1024;

    /**
     * 格式化小数
     */
    private static final DecimalFormat DF = new DecimalFormat("0.00");

    /**
     * 获取文件类型
     */
    public static String getFileType(String extension) {
        String document = "txt doc pdf ppt pps xlsx xls docx";
        String music = "mp3 wav wma mpa ram ra aac aif m4a";
        String video = "avi mpg mpe mpeg asf wmv mov qt rm mp4 flv m4v webm ogv ogg";
        String image = "bmp dib pcp dif wmf gif jpg tif eps psd cdr iff tga pcd mpt png jpeg";

        if (image.contains(extension)) {
            return IMAGE;
        } else if (document.contains(extension)) {
            return TXT;
        } else if (music.contains(extension)) {
            return MUSIC;
        } else if (video.contains(extension)) {
            return VIDEO;
        } else {
            return OTHER;
        }
    }

    /**
     * 文件大小转换
     */
    public static String getSize(long size) {
        String result;
        if (size / GB >= 1) {
            // 如果当前Byte的值大于等于1GB
            result = DF.format(size / (float) GB) + "GB";
        } else if (size / MB >= 1) {
            // 如果当前Byte的值大于等于1MB
            result = DF.format(size / (float) MB) + "MB";
        } else if (size / KB >= 1) {
            // 如果当前Byte的值大于等于1KB
            result = DF.format(size / (float) KB) + "KB";
        } else {
            result = size + "B";
        }
        return result;
    }
}